var data=JSON.parse(sessionStorage.getItem("key"));

console.log("2",data);                     //角色生成对象

var read=JSON.parse(sessionStorage.getItem('playerState'));
console.log('3',read);






for (var i=0; i<data.length;i++){
    var A="\n";
    var main=`<div class="profession-box">
${A}<div class="profession-flex">
${A}<div class="profession-name">${data[i]}</div>
${A}<div class="profession-num">${i+1}号</div>
${A}</div>
${A}<ul class="knife1"><li class="knife"></li></ul></div>` ;

    $(main).appendTo("#main-box");  //根据数组长度，复制盒子
//     $(".knife").show();
}



var a;    //定义一个变量记录被杀手选中的玩家
let q;

let backgroundBox=$('.profession-name');

$('.profession-box').click(function () {


    for(let i=0; i<read.length;i++){
        if(read[i].class==='label'){                                //复位活着的玩家的颜色
            $(backgroundBox[i]).css('background-color','#f5c97b');

            // 隐藏显示小刀
            $('.knife').hide();
            $(this).find('.knife').show();//this函数这里指向被点击盒子
            console.log("333");
            $(this).find('.profession-name').css('background-color','#ff0000');
            q=$('.profession-box').index(this);
            console.log(q);

            a=$(this).index();
            console.log("顺序",a);
            console.log(read[a]);

        }
    }

});

//设置死亡玩家颜色


for (let i = 0; i < read.length; i++) {
    if (read[i].class === "dead") {
        $(backgroundBox[i]).css('background-color', "#ff0000");
    }
}





//胜利判定
function gameOver(){
    var aliveKiller = 0;
    var alivePeople = 0;
    for (let i = 0; i < read.length; i++) {
        if(read[i].class ==="label"){
            if(read[i].name === "幽灵"){
                alivePeople++;
            }else  if(read[i].name ==="杀手"){
                aliveKiller++;
            }
        }
    }

    console.log("存活水民:"+alivePeople);
    console.log("存活杀手:"+aliveKiller);
    switch (true) {
        case aliveKiller ===alivePeople:
            alert("杀手胜利");
            window.location="task4-4.html";   //跳转胜利页面
            break;
        case alivePeople=0:
            alert("杀手胜利");
            window.location="task4-4.html";
            break;
        case aliveKiller=0:
            alert("玩家胜利");
            window.location="task4-4.html";
            break;
    }

}






$('.confirm').click(function () {
    console.log("点击事件");
    console.log(read[q].name );

    if(read[q].name === "幽灵"){
        read[q].class = "dead";//变成死亡状态
        read[q].cause = "已经挂了";//死亡方式，被杀死
        read[q].deathDay = "";//死亡时间
        console.log("哈哈",read);
        window.location="task4-1.html";


    }
    else  if(read[q].name==="杀手"){
        alert("不可以自杀");
    }
    else  if(read[q].class ==="dead"){
        alert("他已经挂了");
    }

    if(a===undefined){
        alert("请选择要投的玩家")
    }else {

    }


    // $(this).find('.profession-name').css('background-color','#ff0000');
    // q=$('.profession-box').index(this);
    // console.log('.profession-name');
    gameOver();
    sessionStorage.setItem("playerState",JSON.stringify(read));
    console.log("hm",read);
});
